package com.ermao.learnWebmagic.processor_2;

import com.ermao.learnWebmagic.config.BilibiliAPIEnum;
import com.ermao.learnWebmagic.config.Config;
import com.ermao.learnWebmagic.entity.UP;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArrayList;

public class UPPageProcessor implements PageProcessor {

	private static final CopyOnWriteArrayList<UP> result = new CopyOnWriteArrayList<>();

	@Override
	public void process(Page page) {
		UPPageHandler upPageHandler = new UPPageHandler();
		upPageHandler.handle(page, result);
	}

	@Override
	public Site getSite() {
		return Site.me().setRetryTimes(Config.RETRY_TIMES).setSleepTime(Config.SLEEP_TIME).setUserAgent(Config.USER_AGENT);
	}

	public static void storeCSV() {
		try (CSVPrinter printer = new CSVPrinter(new FileWriter(Config.UP_CSV_FILE, true), CSVFormat.DEFAULT.withHeader(Config.UP_CSV_FILE_HEADER))) {
			for (UP up : result) {
				printer.printRecord(up.getUp_name(), up.getMid(), up.getFollowers());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	public static void start(String keyword) {
		Spider spider = new Spider(new UPPageProcessor());
		spider.addUrl(BilibiliAPIEnum.getAPI(BilibiliAPIEnum.SEARCH, keyword));
		spider.thread(Config.THREAD_NUM);
		spider.run();
		spider.close();

		storeCSV();
	}
}
